﻿using System;
using System.Configuration;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ParsePtb.util;

namespace ParsePtb
{
    public partial class ParseLogFrm : Form
    {

        private static string sqliteDbLocation = ConfigurationManager.AppSettings["dbRootFolder"].ToString();  //数据库名称
        private static string sqliteDbName = ConfigurationManager.AppSettings["dbName"].ToString(); //数据库存放路径

        public ParseLogFrm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载日志信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParseLogFrm_Load(object sender, EventArgs e)
        {
            this.listDataGriddView.AutoGenerateColumns = false;
            listDataGriddView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            listDataGriddView.EditMode = DataGridViewEditMode.EditOnEnter;
            SetDatePick(ref begin_time, null, true, DatePickChange);
            this.begin_time.Checked = false;

            SetDatePick(ref end_time, null, true, DatePickChange);
            this.end_time.Checked = false;

            LoadData();
        }

        private void DatePickChange(object arg1, EventArgs arg2)
        {
            DateTimePicker picker = arg1 as DateTimePicker;
            bool pickCheck = picker.Checked;

            bool check;
            if ("begin_time".Equals(picker.Name))
            {
                check = this.end_time.Checked;
                this.end_time.MinDate = !pickCheck ? DateTime.Parse("1760-12-01") : picker.Value.AddDays(1);
                this.end_time.Checked = check;
            }

            else if ("end_time".Equals(picker.Name))
            {
                check = this.begin_time.Checked;
                this.begin_time.MaxDate = !pickCheck ? DateTime.Parse("2999-12-01") : picker.Value.AddDays(-1);
                this.begin_time.Checked = check;
            }
        }

        private void LoadData()
        {
            try
            {
                //string colums = "ID,FULL_NAME,FILE_NAME,STATUS,CREATE_TIME,UPDATE_TIME,PARSE_TIME,PARSE_MSG,PARSE_STATUS";
                string colums = "ID,FULL_NAME,FILE_NAME,STATUS,CREATE_TIME,UPDATE_TIME,PARSE_TIME,PARSE_MSG";
                //PARSE
                StringBuilder sbf = new StringBuilder();
                sbf.Append($" select {colums},CASE WHEN PARSE ='1'  THEN '已解析' ELSE '待解析' END AS PARSE");
                sbf.Append($" ,CASE WHEN PARSE_STATUS ='fail'  THEN '解析失败' ELSE '解析成功' END AS PARSE_STATUS from SYS_UPLOAD_FILE_LOG where 1=1");

                if (!string.IsNullOrEmpty(file_name.Text))
                {
                    sbf.Append($" and FILE_NAME like '%{file_name.Text}%'");
                }
                if (!string.IsNullOrEmpty(begin_time.Text) && begin_time.Checked)
                {
                    sbf.Append($" and date(PARSE_TIME) >= date('{begin_time.Text}')");
                }
                if (!string.IsNullOrEmpty(end_time.Text) && end_time.Checked)
                {
                    sbf.Append($" and date(PARSE_TIME) <= date('{end_time.Text}')");
                }

                sbf.Append($" order by PARSE_TIME desc ");

                DataTable dt = SQLiteLibrary.selectDataBySql(sqliteDbLocation, sqliteDbName, sbf.ToString());
                this.listDataGriddView.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                                  "提示",
                                  MessageBoxButtons.OKCancel,
                                  MessageBoxIcon.Warning);

            }
            finally { }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  设置时间选择器的样式
        /// </summary>
        /// <param name="timePicker"></param>
        /// <param name="customFormat"></param>
        /// <param name="allowDrop"></param>
        public static void SetDatePick(ref DateTimePicker timePicker, string customFormat, bool allowDrop, Action<object, EventArgs> changeCall)
        {
            customFormat = customFormat == null ? "yyyy-MM-dd" : customFormat;


            timePicker.CustomFormat = "   ";
            //使用自定义格式
            timePicker.Format = DateTimePickerFormat.Custom;

            //下拉选项是否展示==》默认展示
            timePicker.AllowDrop = allowDrop;

            //timePicker.Text = textValue;

            if (timePicker.Text != null && !"".Equals(timePicker.Text))
            {
                //使用设置的格式
                timePicker.CustomFormat = customFormat;
                timePicker.ShowCheckBox = true;
            }
            else
            {
                timePicker.ShowCheckBox = false;
            }

            if (changeCall != null)
            {
                timePicker.ValueChanged += new EventHandler(changeCall);
            }

        }

        private void SeachBtn_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
